package com.chain.BrotherExchange.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.EntrustQueue;

import java.util.ArrayList;
import java.util.List;

/**
 * 币币交易：涨、跌列表
 */
public class CoinTransactionAdapter extends RecyclerView.Adapter<CoinTransactionAdapter.ViewHolder> {

    private AdapterView.OnItemClickListener mOnItemClickListener;
    private List<EntrustQueue.DataBean.Bean> mCoinTransactionList = new ArrayList<>();

    @NonNull
    @Override
    public CoinTransactionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_transaction_record, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CoinTransactionAdapter.ViewHolder holder, int i) {
        Context context = holder.itemView.getContext();
        EntrustQueue.DataBean.Bean bean = mCoinTransactionList.get(i);
        holder.mPriceTextView.setText(bean.getOn_price());
        holder.mValuationText.setText(bean.getSurplus_num());

        int color = bean.getType() == C.API_ENTRUST_BUY
                ? context.getResources().getColor(R.color.colorDrop)
                : context.getResources().getColor(R.color.colorRosePlate);
        holder.mPriceTextView.setTextColor(color);
        //holder.mValuationText.setTextColor(color);

        holder.itemView.setOnClickListener(v ->
                mOnItemClickListener.onItemClick(null, v, i, 0));
    }

    public EntrustQueue.DataBean.Bean getItem(int position) {
        return mCoinTransactionList.get(position);
    }

    public void setCoinTransactionList(
            List<EntrustQueue.DataBean.Bean> buyBeanList,
            List<EntrustQueue.DataBean.Bean> sellBeanList) {

        mCoinTransactionList = new ArrayList<>();

        if (sellBeanList != null) {
            for (EntrustQueue.DataBean.Bean bean : sellBeanList) {
                bean.setType(C.API_ENTRUST_SELL); // 卖盘
                mCoinTransactionList.add(bean);
            }
        }

        if (buyBeanList != null) {
            for (EntrustQueue.DataBean.Bean bean : buyBeanList) {
                bean.setType(C.API_ENTRUST_BUY); // 买盘
                mCoinTransactionList.add(bean);
            }
        }

        notifyDataSetChanged();
    }

    /**
     * @param type 1-为买盘 2-为卖盘
     */
    public void performOnItemClickListener(int type) {
        if (mCoinTransactionList.size() > 0) {
            int position = 0;
            for (int i = 0; i < mCoinTransactionList.size(); i++) {
                EntrustQueue.DataBean.Bean bean = mCoinTransactionList.get(i);
                if (bean.getType() == type) {
                    position = i;
                    break;
                }
            }

            this.mOnItemClickListener.onItemClick(null, null, position, 0);
        }
    }

    public void setOnItemClickListener(ListView.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return mCoinTransactionList != null ? mCoinTransactionList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mPriceTextView;
        private final TextView mValuationText;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            mPriceTextView = itemView.findViewById(R.id.price_text);
            mValuationText = itemView.findViewById(R.id.valuation_text);
        }
    }
}
